//
// Created by xili on 2024/8/6 20:10.
// Go big or go home.
//
#include <vector>
#include <stack>
#include <climits>

using namespace std;

class Solution {
public:
    bool find132pattern(vector<int> &nums) {
        int n = nums.size();
        //k为132中的2
        int k = INT_MIN;
        stack<int> s;
        for (int i = n - 1; i >= 0; i--) {
            //出现比第二大的值还小的值
            if (nums[i] < k) {
                return true;
            }
            //栈顶维护最大值
            while (!s.empty() && s.top() < nums[i]) {
                //k维护第二大的值
                k = max(k, s.top());
                s.pop();
            }
            s.push(nums[i]);
        }
        return false;
    }
};